package com.whz.gateway.common.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.whz.generic.json.JacksonUtils;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.server.ServerErrorException;

/**
 * @author whz
 */
@Slf4j
@UtilityClass
public class GatewayJsonUtils {
    public static final ObjectMapper MAPPER = JacksonUtils.createDefaultObjectMapper();

    public static String toJson(final Object message) {
        try {
            return MAPPER.writeValueAsString(message);
        } catch (Exception e) {
            log.warn("", e);
            throw new ServerErrorException("", e);
        }
    }


    public static byte[] toJsonByte(final Object message) {
        try {
            return MAPPER.writeValueAsBytes(message);
        } catch (Exception e) {
            log.warn("", e);
            throw new ServerErrorException("", e);
        }
    }
}